package com.fitsoftwareprocess.mobilemap;

import java.util.ArrayList;
import java.util.List;

import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import com.fitsoftwareprocess.mobilemap.bus.AddressFinder;
import com.fitsoftwareprocess.mobilemap.bus.MyWebClient;
import com.fitsoftwareprocess.mobilemap.bus.ShortestPathFinder;
import com.fitsoftwareprocess.mobimap.dto.Coordinate;
import com.fitsoftwareprocess.mobimap.dto.HomeInfoEx;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class ListAddressActivity extends Activity {
	ListView mlistView;
	String keyword;
	List<HomeInfoEx> listHomeInfoEx = new ArrayList<HomeInfoEx>();
	private String fromActivity;
	private String endKeyWord;
	private String startLat;
	private String startLong;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		keyword = getIntent().getExtras().getString("keyword");
		fromActivity = getIntent().getExtras().getString("from");
		endKeyWord = getIntent().getExtras().getString("endkeyword");
		startLong = getIntent().getExtras().getString("long");
		startLat = getIntent().getExtras().getString("lat");
		
		setContentView(R.layout.activity_listaddress);
		AddressFinder addressFinder = new AddressFinder();
		mlistView = (ListView) findViewById(R.id.listaddress_ListAddress);
		listHomeInfoEx = addressFinder.GetListAddress(keyword);
		if(listHomeInfoEx.size()==0)
		{
			//Intent intent = new Intent(ListAddressActivity.this, MainResultActivity.class);
	       // intent.putExtra("keyword", keyword);
	       // intent.putExtra("tab",1);
	       // startActivity(intent);
			
		}
		mlistView.setAdapter(new HomeInfoExItemAdapter(this,
				android.R.layout.simple_list_item_1, listHomeInfoEx));
		mlistView.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				HomeInfoEx temp = (HomeInfoEx) mlistView
						.getItemAtPosition(arg2);
				Intent intent;
				String longitude = temp.get_coordinate().get_longitude() + "";
				String latitude = temp.get_coordinate().get_latitude() + "";
				
				if(fromActivity.equals("ResultStartPointActivity")){
					intent = new Intent(ListAddressActivity.this,
							ResultEndPointActivity.class);
					intent.putExtra("long", longitude);
					intent.putExtra("lat", latitude);
					intent.putExtra("endkeyword", endKeyWord);
					startActivity(intent);
				}else if(fromActivity.equals("ResultEndPointActivity")){
					//Tim duong
					ShortestPathFinder shortestPathFinder = new ShortestPathFinder();
					ArrayList<Coordinate> direction  = shortestPathFinder.FindShortestPath(new Coordinate(Double.parseDouble(startLat), Double.parseDouble(startLong)),
							new Coordinate(Double.parseDouble(latitude), Double.parseDouble(longitude)));
					intent = new Intent(ListAddressActivity.this,
							MainActivity.class);
					intent.putExtra("long", longitude);
					intent.putExtra("lat", latitude);
					intent.putExtra("direction", direction);
					startActivity(intent);
				}else{
					intent = new Intent(ListAddressActivity.this,
							MainActivity.class);
					intent.putExtra("long", longitude);
					intent.putExtra("lat", latitude);
					startActivity(intent);
				}

			}
		});
	}

	class HomeInfoExItemAdapter extends ArrayAdapter<HomeInfoEx> {
		private List<HomeInfoEx> list;

		HomeInfoExItemAdapter(Context context, int textViewResourceId,
				List<HomeInfoEx> list) {
			super(context, textViewResourceId, list);
			this.list = list;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View view = convertView;
			if (view == null) {
				LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				view = vi.inflate(R.layout.listaddressitem, null);
			}

			HomeInfoEx homeInfoEx = list.get(position);
			if (list != null) {

				TextView name = (TextView) view
						.findViewById(R.id.itemaddressfirts);
				name.setText(homeInfoEx.get_area());

			}

			return view;

		}
	}

}
